import {Component, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {DeviceStatus, ChannelOccupy, RecycleStatus} from '../../used-store-manage.pipe';
import {NzModalService} from 'ng-zorro-antd/modal';
import {UsedStorePService} from '../../used-store-p.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {UsedStoreZservice} from '../../used-store-zservice';
import {PermissionService} from '../../../../service/permission.service';

@Component({
  selector: 'app-device-detail',
  templateUrl: './device-detail.component.html',
  styleUrls: ['./device-detail.component.scss']
})
export class DeviceDetailComponent implements OnInit {
  DEVICESTATUS = DeviceStatus;
  CHANNELOCCUPY = ChannelOccupy;
  RECYCLESTATUS = RecycleStatus;
  deviceInfo: any = {};
  // 修改库位模态框
  changeDLModal = {
    visible: false,
    locationId: ''
  };
  // 库位待选项
  locationOptions: any[] = [];
  // 验机报告信息
  reportInfo: any;
  @ViewChild('reportModalRef') reportModalRef: any;

  // 来源options
  sourceOptions = [
    {value: 0, label: '回收'},
    {value: 1, label: '重新入库'}
  ];

  constructor(
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private nzModalService: NzModalService,
    private usedStorePService: UsedStorePService,
    private usedStoreZservice: UsedStoreZservice,
    private nzMessageService: NzMessageService,
    public permissionService: PermissionService
  ) {
  }

  ngOnInit(): void {
    this.usedStoreZservice.recycleLibraryList({page: 1, pageSize: 9999}).then((res: any) => {
      if (res.code === 0) {
        this.locationOptions = res.data.records;
      }
    });
    this.getDeviceInfo();
  }

  getDeviceInfo() {
    this.usedStorePService.getDevicesDetail(this.activatedRoute.snapshot.queryParams.deviceId).then(res => {
      if (res.code === 0) {
        this.deviceInfo = res.data;
        this.deviceInfo.secondhand.imgs = this.deviceInfo.secondhand?.imgs?.split(',');
        this.reportInfo = {
          type: '2',
          id: this.deviceInfo.phoneCheckId,
          okey: this.deviceInfo?.orderCheckVO?.okey,
          previewParams: JSON.stringify(
            {
              moduleId: this.deviceInfo.moduleId, // 机型ID
              evalList: this.deviceInfo.evalList, // 对比项
              umname: this.deviceInfo.umname, // 机型名称
              totalNum: this.deviceInfo.totalNum // 预估合计
            }
          )
        };
      }
    });
  }

  usedInfo() {
    this.router.navigate(['/used-store/usedCommodityInfo'], {queryParams: {id: this.deviceInfo.recycleMachine.goodsId}});
  }

  usedUse() {

  }

  // 放到二手商城销售或者取回
  usedChannel(data: any, status: 0 | 2) {
    const message = status === 2 ? '确认将机器放到二手商城销售？' : '确认取消二手占用？';
    this.nzModalService.confirm({
      nzTitle: '<i>提示</i>',
      nzContent: `<b>${message}</b>`,
      nzOnOk: () => {
        this.usedStorePService.channelOccupy(data.equipmentCode, status).then((res: any) => {
          if (res.code === 0) {
            this.nzMessageService.success('操作成功');
            this.getDeviceInfo();
          }
        });
      }
    });
  }

  // 显示修改库位模态框
  showChangeDPModal() {
    this.changeDLModal = {
      visible: true,
      locationId: this.deviceInfo.recycleMachine.libraryId
    };
  }

  // 修改库位提交
  hideChangeDLModalSubmit() {
    this.usedStorePService.changeDeviceLocation([{
      id: this.deviceInfo.recycleMachine.id,
      libraryId: this.changeDLModal.locationId,
      equipmentCode: this.deviceInfo.recycleMachine.equipmentCode,
    }]).then(res => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getDeviceInfo();
        this.hideChangeDLModal();
      }
    });
  }

  // 隐藏修改库位模态框
  hideChangeDLModal() {
    this.changeDLModal = {
      visible: false,
      locationId: ''
    };
  }

  showReportModal(phoneCode: any) {
    const params = {
      phoneCode
    };
    this.reportModalRef.loadData(params);
    this.reportModalRef.isVisible = true;
  }


  sourceFilter(source: number) {
    let tempObj: any = this.sourceOptions.find((item: any) => item.value === source);
    return tempObj?.label || '-';
  }
}
